package com.e2oto.seller.modules.seller.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.bumptech.glide.Glide;
import com.e2oto.seller.R;
import com.e2oto.seller.common.base.BaseMvpActivity;
import com.e2oto.seller.common.base.FullyLinearLayoutManager;
import com.e2oto.seller.common.utils.BannerUtils;
import com.e2oto.seller.common.utils.ImageLoader;
import com.e2oto.seller.common.utils.RxBus;
import com.e2oto.seller.common.utils.ToastUtils;
import com.e2oto.seller.modules.seller.adapter.PreviewGoodsAdapter;
import com.e2oto.seller.modules.seller.adapter.SpecBatchTagAdapter;
import com.e2oto.seller.modules.seller.adapter.SpecTagAdapter;
import com.e2oto.seller.modules.seller.bean.GoodsInfoBean;
import com.e2oto.seller.modules.seller.bean.PreviewGoodsDetailBean;
import com.e2oto.seller.modules.seller.bean.RxBusBean;
import com.e2oto.seller.modules.seller.presenter.GoodsPreviewPresenter;
import com.e2oto.seller.modules.seller.views.GoodsPreviewView;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.iwf.photopicker.PhotoPreview;

/**
 * Created by ZhangLiQiang on 2017/11/27.
 */

public class GoodsPreviewActivity extends BaseMvpActivity<GoodsPreviewView, GoodsPreviewPresenter> implements GoodsPreviewView {

    @BindView(R.id.goods_preview_banner)
    ConvenientBanner banner;
    @BindView(R.id.preview_goods_rv)
    RecyclerView rv;
    @BindView(R.id.goods_name)
    TextView goods_name;
    @BindView(R.id.goods_old_price)
    TextView goods_old_price;
    @BindView(R.id.goods_price)
    TextView goods_price;
    @BindView(R.id.tag_flow_layout)
    TagFlowLayout flowLayout;
    @BindView(R.id.tag_flow_layout_batch)
    TagFlowLayout flowLayoutBatch;


    private SpecTagAdapter specTagAdapter;//零售规格
    private SpecBatchTagAdapter specBatchTagAdapter;//批发规格
    private PreviewGoodsAdapter adapter;//详情
    private String goodsId;
    private GoodsInfoBean goodsInfoBean;//服务器返回的数据

    @Override
    protected GoodsPreviewPresenter createPresenter() {
        return new GoodsPreviewPresenter();
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_goods_preview;
    }

    @Override
    protected void afterView(Bundle savedInstanceState) {
        super.afterView(savedInstanceState);
        goodsId = getIntent().getStringExtra("goods_id");

        specTagAdapter = new SpecTagAdapter(new ArrayList<GoodsInfoBean.SpecArrayBean>(), this);
        flowLayout.setAdapter(specTagAdapter);

        specBatchTagAdapter = new SpecBatchTagAdapter(new ArrayList<GoodsInfoBean.BatchSpecArrayBean>(), this);
        flowLayoutBatch.setAdapter(specBatchTagAdapter);

        rv.setLayoutManager(new FullyLinearLayoutManager(this));
        rv.setNestedScrollingEnabled(false);
        adapter = new PreviewGoodsAdapter(new ArrayList<String>(), this);
        rv.setAdapter(adapter);

        mPresenter.getGoodsInfo(goodsId);

    }

    @OnClick({R.id.goods_preview_back, R.id.modify_base, R.id.modify_spec, R.id.modify_spec_batch, R.id.modify_detail})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.goods_preview_back:
                finish();
                break;
            case R.id.modify_base:
                Intent step1 = new Intent(this, AddGoodsBaseInfoActivity.class);
                step1.putExtra("data", goodsInfoBean);
                startActivity(step1);
                break;
            case R.id.modify_spec:

            case R.id.modify_spec_batch:
                Intent step2 = new Intent(this, GoodsSpecListActivity.class);
                step2.putExtra("data", goodsInfoBean);
                startActivity(step2);
                break;
            case R.id.modify_detail:
                Intent step3 = new Intent(this, AddGoodsDetailActivity.class);
                step3.putExtra("data", goodsInfoBean);
                startActivity(step3);
                break;
        }
    }

    @Override
    protected void onReceiveRxMessage(RxBusBean anyType) {
        super.onReceiveRxMessage(anyType);
        if (anyType.getType() == 6654) {
            mPresenter.getGoodsInfo(goodsId);
            RxBus.getIntanceBus().post(new RxBusBean(4457));//通知商品列表页面刷新数据
        }
    }

    @Override
    public void showMessage(String msg) {
        ToastUtils.INSTANCE.showToast(this, msg);
    }

    @Override
    public void getGoodsInfoCallback(GoodsInfoBean goodsInfoBean) {
        this.goodsInfoBean = goodsInfoBean;

        goods_name.setText(goodsInfoBean.getName());
        goods_price.setText("¥" + goodsInfoBean.getSell_price());
        goods_old_price.setText("市场价:¥ " + goodsInfoBean.getMarket_price());

        //设置banner
        BannerUtils.setBanner(banner, goodsInfoBean.getCover(), this);

        //详情图
        adapter.getDataList().clear();
        adapter.getDataList().addAll(goodsInfoBean.getContent_images());
        adapter.notifyDataSetChanged();

        //零售规格
        specTagAdapter.getDataList().clear();
        specTagAdapter.getDataList().addAll(goodsInfoBean.getSpec_array());
        specTagAdapter.notifyDataChanged();

        //批发规格
        specBatchTagAdapter.getDataList().clear();
        specBatchTagAdapter.getDataList().addAll(goodsInfoBean.getBatch_spec_array());
        specBatchTagAdapter.notifyDataChanged();
    }


}
